package saer.send.now.operations;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import saer.send.now.channel.Channel;

public class GetId implements IOperation {
    public static final String  OP_CODE = "gi";

    private static final Logger log     = Logger.getLogger(GetId.class.getName());

    @Override
    public void exec(Map<String, Channel> channelStore, String id, HttpServletRequest req, HttpServletResponse resp) {
        log.log(Level.FINEST, "Got GET_ID request, {0}", req);
        try {
            PrintWriter writer = resp.getWriter();
            writer.print(Channel.getNextId());
            writer.close();
        } catch (IOException e) {
            log.log(Level.WARNING, "Failed to send new id to client, e:{}", e);
            throw new RuntimeException("Failed to send new id to client", e);
        }
    }

}
